<?php
	class Login_ctrl extends CI_Controller{
		
		function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->model('login_model');
		}
		
		function index(){
			if(isset($_SESSION['email']) || isset($_COOKIE['email'])){
				redirect('Azindia_home1');
			}
			
		
		
			// Include two files from google-php-client library in controller
			include_once APPPATH . "libraries/google-api-php-client-master/src/Google/Client.php";
			include_once APPPATH . "libraries/google-api-php-client-master/src/Google/autoload.php";
			include_once APPPATH . "libraries/google-api-php-client-master/src/Google/Service/Oauth2.php";

			// Store values in variables from project created in Google Developer Console
			$client_id = '152886452387-lrl6qtsbs6qbrfg6b1q7i52b1ul93m7m.apps.googleusercontent.com';
			$client_secret = '1Fkfu9Vc-8HV_gaFUuZdCR6P';
			$redirect_uri = 'http://localhost/azindia/azindia-client/login_ctrl';
			$simple_api_key = 'AIzaSyAUb01iD5P3J6IERKtE3jtAqvm_ptziQWQ';

			// Create Client Request to access Google API
			$client = new Google_Client();
			$client->setApplicationName("PHP Google OAuth Login Example");
			$client->setClientId($client_id);
			$client->setClientSecret($client_secret);
			$client->setRedirectUri($redirect_uri);
			$client->setDeveloperKey($simple_api_key);
			$client->addScope("https://www.googleapis.com/auth/userinfo.email");

			// Send Client Request
			$objOAuthService = new Google_Service_Oauth2($client);

			// Add Access Token to Session
			if (isset($_GET['code'])) {
			//echo $_GET['code'];exit;
			$client->authenticate($_GET['code']);
			$_SESSION['access_token'] = $client->getAccessToken();
			//echo $_SESSION['access_token'];exit;
			//header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
			}

			// Set Access Token to make Request
			if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
			$client->setAccessToken($_SESSION['access_token']);
			}

			// Get User Data from Google and store them in $data
			if ($client->getAccessToken()) {
			$userData = $objOAuthService->userinfo->get();
			//print_r($userData);exit;
			$this->session->set_userdata($userData);
			//print_r($this->session->userdata($userData));exit;
			$data['userData'] = $userData;
			//print_r($data);exit;
			$_SESSION['access_token'] = $client->getAccessToken();
			redirect('Azindia_home1');
			} else {
			$authUrl = $client->createAuthUrl();
			//echo $authUrl;exit;
			$data['authUrl'] = $authUrl;
			$this->load->view('login_view', $data);
			}
			// Load view and send values stored in $data

		}



		function user_login(){
			$this->load->library('form_validation');
			$this->form_validation->set_error_delimiters("<span style='color:white'>","</span>");
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'required');
			if ($this->form_validation->run() == FALSE) {
			$this->load->view('login_view');
			} else {
			
			$remember_me = $this->input->post('remember_me');
			$pwd = md5($this->input->post('password'));
			$data = array(
			'email' => $this->input->post('email'),
			'password' => $pwd
			);
	
			
			$this->load->model('login_model');
			$result = $this->login_model->user_login($data);
			
			
			if($result !== 0){
				if($remember_me == 'on'){
					setcookie('email', $this->input->post('email'), time()+7200);
					//echo $_COOKIE['email'];exit;
			$this->session->set_userdata($result);
			redirect('Azindia_home1');
			
			}elseif($remember_me == ''){
					$_SESSION['email'] = $this->input->post('email');
			$this->session->set_userdata($result);
			redirect('Azindia_home1');
				}
			
			}elseif($result == 0){
				echo "<script type='text/javascript'>alert('Your account is not activated yet please confirm your mail!');</script>";
				$this->load->view('login_view');
			}else{
				echo "<script type='text/javascript'>alert('User not exist or incorrect password!');</script>";
				$this->load->view('login_view');
			}
		}

		}

		function verify(){
			$this->login_model->verify();
			redirect('login_ctrl');
		}

		function logout(){
			$this->session->sess_destroy();
			setcookie('email', '', time()-7200);
			redirect('login_ctrl');
		}
	}
	
?>